//
//  GBTagListView.m
//  自定义流式标签
//
//  Created by 张国兵 on 15/7/22.
//  Copyright (c) 2015年 zhangguobing. All rights reserved.
//

#import "GBTagListView.h"
@implementation GBTagListView
-(id)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
        
        self.frame=frame;
    }
    return self;
    
}
-(void)setTagWithTagArray:(NSArray*)arr{
    totalHeight=0;
    self.maxWidth = 0;
    while (self.subviews.count) {
        UIView* child = self.subviews.lastObject;
        [child removeFromSuperview];
        child = nil;
    }
    
    previousFrame = CGRectZero;
    __block UILabel *tag;
    NSInteger previousIndex=0;
    [arr enumerateObjectsUsingBlock:^(NSString*str, NSUInteger idx, BOOL *stop) {
        
        tag=[[UILabel alloc]initWithFrame:CGRectZero];
        
        if(_signalTagColor){
            //可以单一设置tag的颜色
            tag.backgroundColor=_signalTagColor;
        }else{
            //tag颜色多样
            tag.backgroundColor=[UIColor colorWithRed:random()%255/255.0 green:random()%255/255.0 blue:random()%255/255.0 alpha:1];
        }
        
        if (_tagColor) {
            tag.textColor=_tagColor;
        }else{
            tag.textColor=[UIColor whiteColor];
        }
        
        if (_tagcornerRadius) {
            tag.layer.cornerRadius=_tagcornerRadius;
        }else{
            tag.layer.cornerRadius=5;
        }
        if (_tagborderWidth) {
            tag.layer.borderWidth=_tagborderWidth;
        }else{
            tag.layer.borderWidth=1;
        }
        if (_tagborderColor) {
            tag.layer.borderColor=_tagborderColor;
        }else{
            tag.layer.borderColor=[UIColor clearColor].CGColor;
        }
        if (_colorsArray) {
            tag.layer.borderColor = [_colorsArray[idx] CGColor];
            tag.textColor = _colorsArray[idx];
        }
        NSDictionary *attrs;
        if (_tagFont) {
            tag.font=_tagFont;
            attrs = @{NSFontAttributeName :_tagFont};
        }else{
            tag.font=[UIFont systemFontOfSize:15];
            attrs = @{NSFontAttributeName : [UIFont systemFontOfSize:15]};
        }
        tag.textAlignment=NSTextAlignmentCenter;
        tag.text=str;
        tag.clipsToBounds=YES;
        CGSize Size_str=[str sizeWithAttributes:attrs];
        Size_str.width += HORIZONTAL_PADDING*2;
        Size_str.height += VERTICAL_PADDING*2;
        
        CGRect newRect = CGRectZero;
        
        if (previousFrame.origin.x + previousFrame.size.width + Size_str.width + LABEL_MARGIN > self.bounds.size.width) {
            
            newRect.origin = CGPointMake(0, previousFrame.origin.y + Size_str.height + BOTTOM_MARGIN);
            totalHeight +=Size_str.height + BOTTOM_MARGIN;
        }
        else {
            //第一行第一个
            if (idx==0) {
                newRect.origin = CGPointMake(0, previousFrame.origin.y);
            }else{
                newRect.origin = CGPointMake(previousFrame.origin.x + previousFrame.size.width + LABEL_MARGIN, previousFrame.origin.y);
            }
        }
        newRect.size = Size_str;
        [tag setFrame:newRect];
        previousFrame=tag.frame;
        self.maxWidth = newRect.origin.x + tag.width;
        
        [self setHight:self andHight:totalHeight+Size_str.height];
//        [self setWidth:self andWidth:self.maxWidth+Size_str.width];
//        DeLog(@"height===%f",totalHeight+Size_str.height);
        [self addSubview:tag];
    }];
    if(_GBbackgroundColor){
        
        self.backgroundColor=_GBbackgroundColor;
        
    } else {
        
        self.backgroundColor=[UIColor whiteColor];
        
    }
}
- (void)setSignalTagColor:(UIColor *)signalTagColor {
    _signalTagColor = signalTagColor;
    for (UILabel *lab_tag in self.subviews) {
        lab_tag.backgroundColor = _signalTagColor;
    }
}
- (void)setColorsArray:(NSArray *)colorsArray {
    _colorsArray = colorsArray;
    for (int i = 0; i < self.subviews.count; i++) {
        UILabel *lab_tag = self.subviews[i];
        lab_tag.layer.borderColor = [_colorsArray[i] CGColor];
        lab_tag.textColor = _colorsArray[i];
    }
}
#pragma mark-改变控件高度
- (void)setHight:(UIView *)view andHight:(CGFloat)height
{
    CGRect tempFrame = view.frame;
    tempFrame.size.height = height;
    view.frame = tempFrame;
}
- (void)setWidth:(UIView *)view andWidth:(CGFloat)width
{
    CGRect tempFrame = view.frame;
    tempFrame.size.width = width;
    view.frame = tempFrame;
}

@end
